package com.example.petstore.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.CartItem;
import com.example.petstore.domain.Item;
import com.example.petstore.mapper.CartItemMapper;
import com.example.petstore.mapper.ItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("cartService")
public class CartService {
    @Autowired
    private CartItemMapper cartItemMapper;
    @Autowired
    private ItemMapper itemMapper;
    public CommonResponse userAddItemToCart(String username, String itemId, int number) {
//        判断库存是否足够
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemid", itemId);
        List<Item> items = itemMapper.selectList(queryWrapper);
        if(items.size() > 0){
            Item item = items.get(0);
            if(item.getQty() < number){
                return CommonResponse.creatForError("库存不足");
            }
        }
//        判断这个商品是否已经在购物车中
        queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("itemid", itemId);
        CartItem cartItem = cartItemMapper.selectOne(queryWrapper);
        if(cartItem != null){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("username", username);
            updateWrapper.eq("itemid", itemId);
            updateWrapper.set("quantity", cartItem.getQuantity() + number);
            int result = cartItemMapper.update(updateWrapper);
            if(result == 1){
                return CommonResponse.creatForSuccess("修改成功");
            }
            return CommonResponse.creatForError("修改失败");
        }else{
            cartItem = new CartItem();
            cartItem.setUsername(username);
            cartItem.setItemid(itemId);
            cartItem.setQuantity(number);
            int result = cartItemMapper.insert(cartItem);
            if(result == 1){
                return CommonResponse.creatForSuccessMessage("修改成功");
            }
            return CommonResponse.creatForError("修改失败");
        }
    }

    public CommonResponse userUpdateCart(String username, String itemId, int number) {
        if(number == 0){
            return userDeleteCartItem(username, itemId);
//            QueryWrapper queryWrapper = new QueryWrapper();
//            queryWrapper.eq("username", username);
//            queryWrapper.eq("itemid", itemId);
//            int result = cartItemMapper.delete(queryWrapper);
//            if(result == 1){
//                return CommonResponse.creatForSuccessMessage("删除成功");
//            }
//            return CommonResponse.creatForError("删除失败");
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("username", updateWrapper);
        updateWrapper.eq("itemid", itemId);
        updateWrapper.set("quantity", number);
        int result = cartItemMapper.update(updateWrapper);
        if(result == 1){
            return CommonResponse.creatForSuccessMessage("修改成功");
        }
        return CommonResponse.creatForError("修改失败");
    }

    public CommonResponse userDeleteCartItem(String username, String itemId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("itemid", itemId);
        int result = cartItemMapper.delete(queryWrapper);
        if(result == 1){
            return CommonResponse.creatForSuccessMessage("删除成功");
        }
        return CommonResponse.creatForError("删除失败");
    }

    public CommonResponse getUerCartItemList(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        List<CartItem> cartItemList = cartItemMapper.selectList(queryWrapper);
        return CommonResponse.creatForSuccess(cartItemList);
//        CommonResponse.
    }
}
